package com.movitech.mobile.cz.base.common.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.HeadersUtils;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.common.utils.CheckResult;
import com.movitech.mobile.cz.base.common.utils.JwtUtils;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;

public class TokenFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        Gson gson = new Gson();
        
        String token = HeadersUtils.getHeadByKey(request, "token");
        String refreshToken = HeadersUtils.getHeadByKey(request, "refresh_token");
        String requestURL = request.getRequestURL().toString();
        if(!StringUtils.containsIgnoreCase(requestURL, "/public/")) {//去除公共的接口方法
        	if(StringUtils.isEmpty(token)) {
            	response.getWriter().write(gson.toJson(new RestReturnResult<Object>(CommonConstants.FAIL,"token不能为空")));
            	return;
            }else {
            	CheckResult checkResult = JwtUtils.parseJWT(token);
            	if(checkResult.isSuccess()) {
            		request.setAttribute("user_info", checkResult.getSubject());
            	}else {
            		if(StringUtils.equalsIgnoreCase(checkResult.getErrorCode(), JwtUtils.JWT_ERRCODE_EXPIRED)) {
            			if(StringUtils.isEmpty(refreshToken)) {
            	        	response.getWriter().write(gson.toJson(new RestReturnResult<Object>(CommonConstants.FAIL,"token验证失败")));
            	        	return;
            	        }else {
            	        	CheckResult checkResultRefresh = JwtUtils.parseJWT(refreshToken);
            	        	if(checkResultRefresh.isSuccess()) {
            	        		request.setAttribute("user_info", true);
            	        		request.setAttribute("session_info", checkResultRefresh.getSubject());
            	        	}else {
            	        		response.getWriter().write(gson.toJson(new RestReturnResult<Object>(CommonConstants.FAIL,"token验证失败")));
                	        	return;
            	        	}
            	        }
            		}else {
            			response.getWriter().write(gson.toJson(new RestReturnResult<Object>(CommonConstants.FAIL,"token不能为空")));
                    	return;
            		}
            	}
            }
        }
        
        filterChain.doFilter(request, response);
	}


	/*   
	springboot注册方式
	@Bean
    public FilterRegistrationBean filterTokenFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        //注入过滤器
        registration.setFilter(new TokenFilter());
        //拦截规则
        registration.addUrlPatterns("/*");
        //过滤器名称
        registration.setName("TokenFilter");
        //过滤器顺序
        registration.setOrder(1);
        return registration;
    }
    */
    
}

